<template>
  <div class="otherOrder">
    <van-sticky z-index="9">
      <div class="pub_tab">
        <van-tabs
          v-model:active="form.activeTab"
          animated
          sticky
          :line-height="8"
          line-width="80"
          title-active-color="#12142E"
          title-inactive-color="#454757"
          color="#FF7A1F"
          background="#f7f7f7"
          @click-tab="onClickTab"
        >
          <van-tab
            :title="`${item.title}`"
            v-for="(item,index) in tabbar"
            :key="index"
          />
        </van-tabs>
      </div>
    </van-sticky>
    <div class="content_box">
      <van-pull-refresh
        disabled
        v-model="refreshing"
        @refresh="onRefresh"
      >
        <van-list
          v-model="loading"
          :finished="finished"
          @load="loadMore"
          :immediate-check="false"
        >
          <div class="order">
            <div class="orderBox">
              <div
                class="orderInfo"
                v-for="(item,index) in demandList"
                :key="index"
              >
                <div class="orderInfoBox">
                  <p
                    class="line25 title"
                    v-if="item.status == 1"
                  >
                    <van-tag
                      style="height:19px;width:55px;justify-content: center;"
                      class="tagleft"
                      color="#FF7A1F"
                      type="primary"
                    >
                      未开发票
                    </van-tag>
                    <span class="content3 fontBold color3">{{ item.title }}</span>
                  </p>
                  <p
                    class="line25 title"
                    v-if="item.status == 2"
                  >
                    <van-tag
                      style="height:19px;width:55px;justify-content: center;"
                      class="tagleft"
                      color="#2A90EF"
                      type="primary"
                    >
                      待开发票
                    </van-tag>
                    <span class="content3 fontBold color3">{{ item.title }}</span>
                  </p>
                  <p
                    class="line25 title"
                    v-if="item.status == 3"
                  >
                    <van-tag
                      style="height:19px;width:55px;justify-content: center;"
                      class="tagleft"
                      color="#85898C"
                      type="primary"
                    >
                      已开票
                    </van-tag>
                    <span class="content3 fontBold color3">{{ item.title }}</span>
                  </p>
                  <p class="moeny flex align_center justify_between">
                    <span class="content2 font400 color6">{{ item.create_time }}</span>
                    <span class="content3 font400 color1">￥{{ item.price }}</span>
                  </p>
                </div>
                <div class="confin flex align_center justify_between">
                  <div
                    @click="Invoicing(item.status, item.id)"
                    class="confinItem confinItemW see font400 content2 color2"
                  >
                    <span v-if="item.status == 1">去开票</span>
                    <span v-if="item.status == 2">发票信息</span>
                    <span v-if="item.status == 3">发票信息</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <vant-list />
        </van-list>
      </van-pull-refresh>
    </div>
    <no-data v-if="demandList.length === 0" />
  </div>
  <div class="fexd_footer">
    <div class="footer_btn flex align_center justify_between">
      <div class="flex align_center">
        <div
          @click="back"
          class="agree font400 content3 color4"
        >
          返回
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { publishList } from '@/api'
import { ref, computed, watch, defineAsyncComponent } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useStore } from 'vuex'
const NoData = defineAsyncComponent(() => import('@/components/NoData/index.vue'))
const router = useRouter()
const route = useRoute()
const store = useStore()
const loading: any = ref(false)
const finished: any = ref(false)
const refreshing: any = ref(false)
const demandList: any = ref([])
// const list: any = ref([])
const tabbar: any = ref([
  {
    title: '未开发票'
  },
  {
    title: '已开发票'
  }
])

const form: any = ref({
  page: 1,
  pageSize: 10,
  activeTab: Number(route.query.s) || 0
})

const selectedDemandTypeId = computed(() => {
  return Number(route.query.s) || 0
})

watch(selectedDemandTypeId, (newVal, oldVal) => {
  onRefresh()
})

const onClickTab = async (item: any) => {
  router.replace({
    path: '/otherServices/cost',
    query: { s: item.name }
  })
  form.value.activeTab = Number(item.name)
}

const back = () => {
  if (store.state.from.path !== '/') {
    router.back()
  } else {
    router.replace('/')
  }
}

const onRefresh = async () => {
  form.value.page = 1
  const list:any = await loadDemandList()
  demandList.value = list
  refreshing.value = false
}

const loadMore = async () => {
  form.value.page = form.value.page + 1
  const list:any = await loadDemandList()
  demandList.value = new Set([...demandList.value, ...list])
  loading.value = false
}

const loadDemandList = async () => {
  // 异步更新数据
  const res:any =
    (await publishList({ ...form.value, type: Number(form.value.activeTab + 1) })) || []
  if (!Array.isArray(res) && typeof res.list === 'object') {
    const keys:any = Object.keys(res.list)
    const newRes:any = keys.map((k:any) => res.list[k])
    finished.value = newRes.length < form.value.pageSize
    console.log('newRes', newRes)
    return newRes
  }

  finished.value = res.list.length < form.value.pageSize
  return res
}

onRefresh()

const Invoicing = async (type:any, id:any) => {
  if (type === '1') {
    router.push({ path: '/otherServices/Invoicing', query: { type: type, id: id } })
  } else if (type === '2' || type === '3') {
    router.push({ path: '/otherServices/information', query: { type: type, id: id } })
  }
}

</script>
<style lang="less" scoped>
.otherOrder {
  padding: 15px 0 94px;
}
.order {
  width: 314px;
  margin: 20px auto 0;
  .orderBox {
    margin-bottom: 15px;
    .orderInfo {
      background: #ffffff;
      border-radius: 5px;
      margin-bottom: 15px;
      .orderInfoBox {
        padding: 15px 15px 0;
        .title {
          height: 40px;
        }
        .tagleft {
          margin-right: 5px;
        }
        .moeny {
          margin-top: 20px;
          margin-bottom: 15px;
        }
      }
    }
  }
  .confin {
    border-top: 1px solid #cacaca;
    .confinItem {
      width: 50%;
      text-align: center;
      height: 48px;
      line-height: 48px;
      border-right: 1px solid #cacaca;
    }
    .confinItemW {
      width: 100%;
    }
    .confinItem:last-child {
      border-right: 0;
    }
  }
}
.fexd_footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 70px;
    background-color: #fff;
    box-shadow: 0px 2px 12px 2px rgba(22, 12, 6, 0.09);
    .footer_btn {
        padding: 0 30px;
        height: 100%;
        text-align: center;
        .agree {
            width: 315px;
            height: 44px;
            background: #85898C;
            border-radius: 5px;
            text-align: center;
            line-height: 44px;
        }
    }
}
@media (min-width: 750px) {
  .otherOrder {
  padding: 15px 0 94px;
}
.order {
  width: 314px;
  margin: 20px auto 0;
  .orderBox {
    margin-bottom: 15px;
    .orderInfo {
      background: #ffffff;
      border-radius: 5px;
      margin-bottom: 15px;
      .orderInfoBox {
        padding: 15px 15px 0;
        .title {
          height: 40px;
        }
        .tagleft {
          margin-right: 5px;
        }
        .moeny {
          margin-top: 20px;
          margin-bottom: 15px;
        }
      }
    }
  }
  .confin {
    border-top: 1px solid #cacaca;
    .confinItem {
      width: 50%;
      text-align: center;
      height: 48px;
      line-height: 48px;
      border-right: 1px solid #cacaca;
    }
    .confinItemW {
      width: 100%;
    }
    .confinItem:last-child {
      border-right: 0;
    }
  }
}
.fexd_footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 70px;
    background-color: #fff;
    box-shadow: 0px 2px 12px 2px rgba(22, 12, 6, 0.09);
    .footer_btn {
        padding: 0 30px;
        height: 100%;
        text-align: center;
        .agree {
            width: 315px;
            height: 44px;
            background: #85898C;
            border-radius: 5px;
            text-align: center;
            line-height: 44px;
        }
    }
}
}
</style>
